<?php
/**
 * 前台使用的 应用 相关数据获取方法
 * Created by PhpStorm.
 * User: samxiao
 * Date: 2017/6/9
 * Time: 下午5:58
 */

namespace App\Repositories;

use App\Models\CMS\Category;
use App\Models\CMS\Content;
use App\Models\NodeTag;
use App\Models\Product;
use App\Models\TravelNote;
use Illuminate\Support\Facades\DB;

class AppRepository
{
    /**
     * @var array
     */
    private $properties = null;
    const CACHE_TAG = 'content-list';

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getNavByParentId($id)
    {
        return DB::table('cms_navigations')->where(['parent_id' => $id])->whereNull('deleted_at')->get();
    }

    /**
     * @param $path
     * @return string
     */
    public function getImageHome($path)
    {
        $thumbBaseUrl = 'http://' . env('IMG_ENDPOINT') . '/';
        return $thumbBaseUrl . $path;
    }

    /**
     * @param     $content
     * @param int $number
     * @return string
     */
    public function standard($content, $number = 20)
    {
        if (mb_strlen($content) > 20) {
            return $this->utf8Substr($content, 1, $number) . '...';
        }
        return $content;
    }

    /**
     * @param $str
     * @param $from
     * @param $len
     * @return null|string|string[]
     */
    private function utf8Substr($str, $from, $len)
    {
        return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' .
            '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s',
            '$1', $str);
    }

    /**
     * @param $string
     * @return null|string|string[]
     */
    public function getString($string)
    {
        return strip_tags($string);
    }

    /**
     * @param string $type
     * @return array|\Illuminate\Support\Collection
     */
    public function getProperty($type = 'site-property')
    {
        if (empty($this->properties)) {
            $this->properties = Property::query()->where('type', $type)->pluck('value', 'key');
        }
        return $this->properties;
    }

    public function getNameDesc($string)
    {
        return explode('  ', $string);
    }

    /**
     * @param $number
     * @param $columns
     * @return mixed
     */
    public function getPictureByColumn($number, $columns)
    {

        $value = \Cache::tags(['cms-content'])->remember(json_encode($columns), 100,
            function () use ($columns, $number) {
                $query = Content::query()
                    ->where(['content_detail_type'=>'ContentPicture'])
                    ->with(['content_detail'])
                    ->orderBy('id')
                    ->limit($number);
                if($columns){
                    $query->whereHas('category', function ($query) use ($columns) {
                        $query->where('full_path', 'like','%'.implode('/',$columns).'%');
                    });

                    return $query->get();
                }else{
                    return [];
                }
            });
        return $value;
    }



    public function getMediaByColumn($number, $columns)
    {

        $value = \Cache::tags(['cms-content'])->remember(json_encode($columns), 100,
            function () use ($columns, $number) {
                $query = Content::query()
                    ->where(['content_detail_type'=>'ContentMedia'])
                    ->with(['content_detail'])
                    ->orderBy('id')
                    ->limit($number);
                if($columns){
                    $query->whereHas('category', function ($query) use ($columns) {
                        $query->where('full_path', 'like','%'.implode('/',$columns).'%');
                    });

                    return $query->get();
                }else{
                    return [];
                }
            });
        return $value;
    }


    public function getContentByColumn($number, $columns)
    {

        $value = \Cache::tags(['cms-content'])->remember(json_encode($columns), 100,
            function () use ($columns, $number) {
                $query = Content::query()
                    ->where(['content_detail_type'=>'ContentBlog'])
                    ->with(['content_detail'])
                    ->orderBy('id')
                    ->limit($number);
                if($columns){
                    $query->whereHas('category', function ($query) use ($columns) {
                        $query->where('full_path', 'like','%'.implode('/',$columns).'%');
                    });

                    return $query->get();
                }else{
                    return [];
                }
            });
        return $value;
    }


    /**
     * @return mixed
     */
    public function getTags(){
        $value = \Cache::tags(['cms-content'])->remember('home-tags', 100,function (){
            return DB::table('travel_tags')->limit(4)->get();
        });
        return $value;
    }


    public function getRecommendProduct(){
        $value = \Cache::tags(['cms-content'])->remember('recommend-product', 100,function (){
            return Product::query()
                ->where('is_recommend',1)
                ->limit(8)
                ->with('way','images')->get();
        });
        return $value;
    }
    public function getLikeProduct(){
        $value = \Cache::tags(['cms-content'])->remember('like-product', 100,function (){
            return Product::query()
                ->limit(8)
                ->with('way','images')->get();
        });
        return $value;
    }
    public function getTravelNote(){
        $value = \Cache::tags(['cms-content'])->remember('travel-note', 100,function (){
            return TravelNote::query()
                ->with('product.way','asset','user')
                ->where(['is_public'=>1,'status'=>1])
                ->limit(4)
                ->get();
        });
        return $value;
    }

    public function getNoteTag(){
        $value = \Cache::tags(['cms-content'])->remember('note-tag', 100,function (){
            return NodeTag::query()
                ->limit(4)
                ->get();
        });
        return $value;
    }

}
